﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using Aptech.UI;
using Remind.Model;
using Remind.BLL;


namespace Remind.View
{
    public partial class FrmAddContact : Form
    {
        int cid = 0;
        public static int faceid = 0;
        //private SideBar _sidebar = null;
        private ImageList _imglist = null;
        private TelBLL tBLL = new TelBLL();
        private GroupBLL gBLL = new GroupBLL();
        private AddressBLL aBLL = new AddressBLL();
        private NetworkBLL nBLL = new NetworkBLL();
        private ContactsBLL cBLL = new ContactsBLL();

        public FrmAddContact()
        {
            InitializeComponent();
        }

        public FrmAddContact(ImageList imgList)
        {
            InitializeComponent();
            _imglist = imgList;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (ValidateInfo())
            {
                //插入联系人信息
                ContactsInfo contact = PackageContactsInfo();
                if (cBLL.AddContactInfo(contact) > 0)
                {
                    //查询刚刚插入的联系人id
                    cid = cBLL.GetMaxCid();
                    //编辑刚刚插入的联系人的信息
                    TelInfo tel = PackageTelInfo();
                    AddressInfo addr = PackageAddressInfo();
                    NetworkInfo network = PachageNetworkInfo();
                    //查询其他信息
                    int resoult = 0;
                    resoult = tBLL.InsertTel(tel);
                    resoult += aBLL.InsertAddress(addr);
                    resoult += nBLL.InsertNetwork(network);
                    if (resoult == 3)
                    {
                        MessageBox.Show("添加成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Close();
                    }
                }
            }
        }
        //封装联系人
        private ContactsInfo PackageContactsInfo()
        {
            ContactsInfo contact = new ContactsInfo();

            contact.Gid = (int)cmbGroup.SelectedValue;
            contact.Cbirthday = dtBirthday.Value;
            contact.Cname = txtName.Text;
            contact.Csex = cmbSex.SelectedItem.ToString() == "女" ? false : true;
            contact.IdCard = txtIDCard.Text;
            contact.Cface = faceid;
            contact.CBz = txtBz.Text;
            return contact;
        }
        //封装电话信息
        private TelInfo PackageTelInfo()
        {
            TelInfo tel = new TelInfo();
            tel.Cid = cid;
            tel.Phone = txtPhone.Text;
            tel.Phone1 = txtPhone1.Text;
            tel.Family = txtFamily.Text;
            tel.Company = txtCompany.Text;
            tel.Fax = txtFax.Text;
            return tel;
        }
        //封装地址信息
        private AddressInfo PackageAddressInfo()
        {
            AddressInfo add = new AddressInfo();
            add.Cid = cid;
            add.Company = txtAddrCompany.Text;
            add.Family = txtAddrFamily.Text;
            add.Other = txtAddrOther.Text;
            return add;
        }
        //封装网络信息
        private NetworkInfo PachageNetworkInfo()
        {
            NetworkInfo network = new NetworkInfo();
            network.Cid = cid;
            network.Email = txtEmail.Text;
            network.QQ = txtQQ.Text;
            network.Msn = txtMsn.Text;
            network.Blog = txtBlog.Text;
            network.WebSite = txtWebsite.Text;
            return network;
        }

        private void FrmAddContact_Load(object sender, EventArgs e)
        {
            //生日的最大时间是今天
            this.dtBirthday.MaxDate = DateTime.Now;
            this.dtBirthday.Value = Convert.ToDateTime("1988-08-06");
            //性别默认男
            this.cmbSex.SelectedIndex = 0;
            //绑定分组
            cmbGroup.DataSource = gBLL.GetGroup();
            cmbGroup.DisplayMember = "gname";
            cmbGroup.ValueMember = "gid";
            //默认第一个
            cmbGroup.SelectedIndex = 0;
        }
        //验证信息
        private bool ValidateInfo()
        {
            Regex rx = new Regex(@"^1[358]\d{9}$");

            if (string.IsNullOrEmpty(txtName.Text.Trim()))
            {
                MessageBox.Show("*号项是必填字段，请输入联系人姓名！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtName.Focus();
                return false;
            }

            else if (!rx.IsMatch(txtPhone.Text) && !string.IsNullOrEmpty(txtPhone.Text.Trim()))
            {
                MessageBox.Show("手机号码请输入11位数字！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtPhone.Focus();
                return false;
            }
            else if (!rx.IsMatch(txtPhone1.Text) && !string.IsNullOrEmpty(txtPhone1.Text.Trim()))
            {
                MessageBox.Show("手机号码请输入11位数字！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtPhone1.Focus();
                return false;
            }
            else if (!Regex.IsMatch(txtEmail.Text, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$") && !string.IsNullOrEmpty(txtEmail.Text.Trim()))
            {
                MessageBox.Show("您输入的电子邮件格式有误，请重新输入！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtEmail.Focus();
                return false;
            }
            return true;
        }

        private void dtBirthday_ValueChanged(object sender, EventArgs e)
        {
            lblAge.Text = (DateTime.Now.Year - dtBirthday.Value.Year).ToString();
        }

        private void btnChangeface_Click(object sender, EventArgs e)
        {
            //把当前的头像组传递给头像管理窗口
            FrmFaceManager ffmage = new FrmFaceManager(_imglist, picboxinfo);
            try
            {
                ffmage.ShowDialog(this);
            }
            finally
            {
                ffmage.Dispose();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
